package framework.view;

import java.util.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

import framework.controller.*;
import framework.model.*;

public class PlayersView extends InteractiveView {

	//Menu width
	private final int MENU_WIDTH = 200;
	// Panels
	private JPanel playerInfoPanel;
	// ScrollPane
	private JScrollPane playersListScrollPane;
	// ComboBox
	private JComboBox<String> gamesList;
	// Buttons
	private JButton playerInfoButton, playerListRefreshButton;
	// List
	private JList<String> playersList;
	
	/**********************
	 *** GLOBAL METHODS ***
	 *********************/
	
	/**
	 * Initialize PlayersView
	 * @method PlayersView (Contructor)
	 * @param model
	 * @param controller
	 */
	public PlayersView(Model model, Controller controller) {
		//Call parent
		super(model, controller);
		//Initialize PlayersView
		this.init();
		//Players List
		this.setPlayersList();
		this.setPlayersListScrollPane();
		//Set games List
		this.setGamesList();
		//Set player panel
		this.setPlayerButtons();
		this.setPlayerInfoPanel();
		//Set main panels
		this.setPanels();
	};
	
	@Override
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand()) {
			case "players_updated" :
				//Update players list
				this.setPlayersListData(this.getModel().getPlayers());
			break;
			case "games_updated" : 
				//Update games list
				this.setGamesListData(this.getModel().getGames());
			break;
			case "refresh" : 
				//Players list update request
				this.getController().handlePlayersRequest();
			break;
			case "challenge" : 
				//Players list update request
				this.getController().handleChallengeRequest(
					this.playersList.getSelectedValue(), //SELECTED PLAYER
					this.gamesList.getSelectedItem().toString() //SELECTED GAME
				);
			break;
		};
	}
	
	/**********************
	 *** SETTER METHODS ***
	 *********************/
	
	/**
	 * Set list for player data from GameServer
	 * @method setPlayersList
	 */
	private void setPlayersList() {
		this.playersList = new JList<String>();
		this.playersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.playersList.setLayoutOrientation(JList.VERTICAL);
		this.playersList.setVisibleRowCount(-1);
		this.playersList.setBackground(Color.LIGHT_GRAY);	
	};
	
	private void setPlayersListData(ArrayList<String> players) {
		String list[] = new String[players.size()];
		this.playersList.setListData(players.toArray(list));
		this.playersList.setSelectedIndex(0);
	}
	
	private void setGamesList() {
		this.gamesList = new JComboBox<String>();
	};
	
	private void setGamesListData(ArrayList<String> games) {
		this.gamesList.removeAll();
		for(String game : games) {
			this.gamesList.addItem(game);
		}
	};
	
	/**
	 * Set player-info button and player-list-refresh button
	 * @method setButtons
	 */
	private void setPlayerButtons() {
		//Player info button
		this.playerInfoButton = new JButton("challenge");
		this.playerInfoButton.setActionCommand("challenge");
		this.playerInfoButton.addActionListener(this);
		//Players refresh button
		this.playerListRefreshButton = new JButton("refresh");
		this.playerListRefreshButton.setActionCommand("refresh");
		this.playerListRefreshButton.addActionListener(this);
	};
	
	/**
	 * Set info panel for player data
	 * @method setPlayerInfoPanel
	 */
	private void setPlayerInfoPanel() {
		this.playerInfoPanel = new JPanel(new GridLayout(2,1,0,0));
		this.playerInfoPanel.setPreferredSize(new Dimension(MENU_WIDTH,60));	
		this.playerInfoPanel.add(this.gamesList);
		this.playerInfoPanel.add(this.playerInfoButton);
	};
	
	private void setPanels() {
		super.add(this.playerInfoPanel, BorderLayout.NORTH);
		super.add(this.playersListScrollPane,BorderLayout.CENTER);
		super.add(this.playerListRefreshButton, BorderLayout.SOUTH);
	};
	
	private void setPlayersListScrollPane() {
		this.playersListScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		this.playersListScrollPane.setBackground(Color.blue);
		this.playersListScrollPane.setViewportView(this.playersList);
	};

	private void init() {
		this.setSize(new Dimension(MENU_WIDTH,600));
		this.setPreferredSize(new Dimension(MENU_WIDTH,600));
		this.setLayout(new BorderLayout(0,0));
	}

	/**
	 * Override parent method for getting LobbyController 
	 * @method getController
	 * @return <LoginController> controller
	 */
	@Override
	public LobbyController getController() {
		return (LobbyController) super.getController();
	};
	
	/**
	 * Override parent method for getting LobbyModel  
	 * @method getModel
	 * @return <LobyModel> model
	 */
	@Override
	protected LobbyModel getModel() {
		return (LobbyModel) super.getModel();
	};
	
}
